package table

import (
	"gitee.com/linxing_3/sr-core/domain/api"
	"gitee.com/linxing_3/sr-core/domain/service/srvctx"
	"gitee.com/mkwchecking/sr-admin/apps/devtools/acl"
	"github.com/spf13/cobra"
)

var (
	_tableNewArg = &TableCreateArg{}
	_tableNewCmd = &cobra.Command{
		Use:     "new",
		Short:   "new table",
		PreRunE: dependsCheck,
		RunE:    createTableHandler,
	}

	_tableDeleteArg = &TableDeleteArg{}
	_tableDeleteCmd = &cobra.Command{
		Use:     "del",
		Short:   "delete table",
		PreRunE: dependsCheck,
		RunE:    deleteTableHandler,
	}

	_tableListArg = &TableListArg{}
	_tableListCmd = &cobra.Command{
		Use:     "list",
		Short:   "list table",
		PreRunE: dependsCheck,
		RunE:    listTableHandler,
	}

	_tableUpdateArg = &TableCreateArg{}
	_tableUpdateCmd = &cobra.Command{
		Use:     "update",
		Short:   "update table",
		PreRunE: dependsCheck,
		RunE:    updateTableHandler,
	}

	_tableImportArg = &TableImportArg{}
	_tableImportCmd = &cobra.Command{
		Use:     "import",
		Short:   "import table",
		PreRunE: dependsCheck,
		RunE:    importTableHandler,
	}
)

func init() {
	tableCmd.AddCommand(_tableDeleteCmd)
	_tableDeleteArg.Parse(_tableDeleteCmd)

	tableCmd.AddCommand(_tableNewCmd)
	_tableNewArg.Parse(_tableNewCmd)

	tableCmd.AddCommand(_tableListCmd)
	_tableListArg.Parse(_tableListCmd)

	tableCmd.AddCommand(_tableUpdateCmd)
	_tableUpdateArg.Parse(_tableUpdateCmd)

	tableCmd.AddCommand(_tableImportCmd)
	_tableImportArg.Parse(_tableImportCmd)
}

func createTableHandler(cmd *cobra.Command, args []string) error {

	if err := api.VerifyRequest(_tableNewArg); err != nil {
		return err
	}

	ctx := srvctx.NewCmdCtx(cmd)

	req := &acl.ModuleTableInsertReq{}
	req.ClassName = _tableNewArg.GetName()
	req.Domain = _tableNewArg.GetDomain()

	err := svc.Table.Insert(ctx, req)
	return err
}

func deleteTableHandler(cmd *cobra.Command, args []string) error {

	if err := api.VerifyRequest(_tableDeleteArg); err != nil {
		return err
	}

	ctx := srvctx.NewCmdCtx(cmd)
	req := &acl.ModuleTableDeleteReq{}
	req.ClassName = _tableDeleteArg.GetName()
	req.Domain = _tableDeleteArg.GetDomain()

	err := svc.Table.Delete(ctx, req)
	return err
}

func listTableHandler(cmd *cobra.Command, args []string) error {

	ctx := srvctx.NewCmdCtx(cmd)
	req := &acl.ModuleTableListReq{}
	req.Domain = _tableListArg.GetDomain()
	req.PageNum = 1
	req.PageSize = 100000

	res, _, err := svc.Table.List(ctx, req)
	if err != nil {
		return err
	}

	for idx, t := range res {
		if _tableListArg.Format == "json" {
			PrintAsJson(t, idx == 0, idx == len(res)-1)
		} else {
			PrintAsTable(t, idx == 0, idx == len(res)-1)
		}
	}
	return nil
}

func updateTableHandler(cmd *cobra.Command, args []string) error {
	ctx := srvctx.NewCmdCtx(cmd)
	req := &acl.ModuleTableUpdateReq{}
	req.Domain = _tableUpdateArg.GetDomain()
	req.ClassName = _tableUpdateArg.GetName()
	req.Comment = _tableUpdateArg.Comment

	err := svc.Table.Update(ctx, req)
	return err
}

func importTableHandler(cmd *cobra.Command, args []string) error {
	ctx := srvctx.NewCmdCtx(cmd)
	req := &acl.ImportDBTable{}
	req.Schema = _tableImportArg.Domain
	req.TableName = _tableImportArg.Name
	err := svc.Table.InsertFromDB(ctx, req)
	return err
}
